using Microsoft.EntityFrameworkCore;
using UpdaterManagement.Models;

namespace UpdaterManagement.Data
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }

        public DbSet<Product> Products { get; set; } = null!;
        public DbSet<VersionRelease> VersionReleases { get; set; } = null!;
        public DbSet<DeployedApplication> DeployedApplications { get; set; } = null!;
        public DbSet<Department> Departments { get; set; } = null!;
        public DbSet<User> Users { get; set; } = null!;
        public DbSet<Enterprise> Enterprises { get; set; } = null!;

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 配置VersionRelease与Enterprise的关系
            modelBuilder.Entity<VersionRelease>()
                .HasOne(v => v.Enterprise)
                .WithMany()
                .HasForeignKey(v => v.EnterpriseId)
                .OnDelete(DeleteBehavior.Restrict);
            
            // 配置DeployedApplication与Enterprise的关系
            modelBuilder.Entity<DeployedApplication>()
                .HasOne(d => d.Enterprise)
                .WithMany()
                .HasForeignKey(d => d.EnterpriseId)
                .OnDelete(DeleteBehavior.Restrict);
            
            // 配置DeployedApplication与Product的关系
            modelBuilder.Entity<DeployedApplication>()
                .HasOne(d => d.Product)
                .WithMany()
                .HasForeignKey(d => d.ProductId)
                .OnDelete(DeleteBehavior.Restrict);
            
            // 配置Product与Department的关系
            modelBuilder.Entity<Product>()
                .HasOne(p => p.Department)
                .WithMany()
                .HasForeignKey(p => p.DepartmentId)
                .OnDelete(DeleteBehavior.Restrict);
        }
    }
}